package com.test

import com.entity.PackPath
import com.entity.toPackPath
import com.subpackage.ContrastFile
import com.utils.YamlUtil
import java.nio.file.FileSystems
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.absolute
import kotlin.io.path.extension
import kotlin.io.path.pathString
import kotlin.test.Test

class PathTest {


    @Test
    fun test() {

        val wildcardPattern = "**/config*.js"

        val paths = listOf(
            PackPath("E:/Project/packTest/old/config.js"),
            PackPath("E:/Project/packTest/old/config_fr.js"),
            PackPath("E:/Project/packTest/old/configab.js"),
            PackPath("E:/Project/packTest/old/heconfig.js"),
            PackPath("E:/Project/packTest/old/he_config.js"),
            PackPath("E:/Project/packTest/old/config.js/a.js"),
            PackPath("E:/Project/packTest/old/config/b.js"),
            PackPath("E:/Project/packTest/old/configww/c.js"),
        )

        val pattern = if (wildcardPattern.startsWith("glob:"))
            wildcardPattern else "glob:$wildcardPattern"

        val pathMatcher = FileSystems.getDefault().getPathMatcher(pattern)
        paths.forEach {

            val result = pathMatcher.matches(it.toPath)
            println("${it.name}  $result  ")
        }


    }


    @Test
    fun testPathResolve() {
        var pathA = Path("E:/Project/packTest/old")
        var pathB = PackPath("../assets/web.config")


        pathB.replaceDirectory(pathA.pathString)
        pathB.normalize()
        println(
            pathB.absolutePathString
        )
    }


    @Test
    fun testRelativePath() {
        YamlUtil.loadYaml("./config/config2.yml")
        val contrastFile = ContrastFile()
        println(contrastFile.getRelativePath("E:/Project/packTest/old/assets/index.js"))
        println(contrastFile.getRelativePath("E:/Project/packTest/old/test/index.js"))
        println(contrastFile.getRelativePath("E:/Project/packTest/old/index.js"))
        println(contrastFile.getRelativePath("E:/Project/packTest/old/index.js", true))

    }

    @Test
    fun test2() {

//        val wildcardPattern = "{**/,,**/e}index{!.min,}.js"
        val wildcardPattern = "**/*{!.min,}.js"

        val paths = listOf(
            PackPath("index.js"),
            PackPath("D:/test/index.js"),
            PackPath("D:/test/index.min.js"),
            PackPath("D:/test/eindex.js"),
            PackPath("D:/test/eaindex.js"),
        )
        val pattern = if (wildcardPattern.startsWith("glob:"))
            wildcardPattern else "glob:$wildcardPattern"

        val pathMatcher = FileSystems.getDefault().getPathMatcher(pattern)
        paths.forEach {

            val result = pathMatcher.matches(it.toPath)
            println("${it.name}  $result  ")
        }


    }

    @Test
    fun test3() {
        println(
            Path("index.min.js").extension
        )
    }

    @Test
    fun testSort() {

        val pathStr = "D:/WorkSpace/Idea/TypeScript/CrashGames/KickersX/assets"


        val path = PackPath(pathStr)

        val file = path.walk().toList()
        val scanList = file.joinToString("\n")

        PackPath("out/scan.txt").writeText(scanList)


        val fileList = file.toMutableList()



        fileList.shuffle()


        PackPath("out/shuffle.txt").writeText(fileList.joinToString("\n"))

        // 按照目录层级和文件名进行排序
        val sortedFileList = fileList.map {
            it to it.toList()
        }.sortedWith { pair1, pair2 ->
            val components1 = pair1.second
            val components2 = pair2.second
            val minSize = minOf(components1.size, components2.size)
            for (i in 0 until minSize) {
                val cmp = components1[i].compareTo(components2[i])
                if (cmp != 0) return@sortedWith cmp
            }
            components1.size.compareTo(components2.size)
        }.map { it.first }

        PackPath("out/sorted.txt").writeText(sortedFileList.joinToString("\n"))


    }

    @Test
    fun testNormalize() {
        println(Path("../a").absolute().normalize())
        println(Path("./").toAbsolutePath().normalize())
    }


}